import java.util.*;

public class Spoj0{
	
	public static void main (String args[]){
			
	Scanner scanner = new Scanner(System.in);
    int numAcc=scanner.nextInt(); //number of accounts
    int numPro=scanner.nextInt(); //number of problems
    String string=new String(""); // user and problems
    String[] array=new String[numAcc];//
	int counter0,counter1;
	counter0=0;
    
    while(numAcc>0){
    	scanner.nextLine();
    	array[counter0]=scanner.next();
    	counter1=0;
    	
    	for (int i=numPro;i>0;i--){
    		if (scanner.nextInt()==1)counter1++;
    		 else;
    	}
    	
    	array[counter0]=array[counter0]+Integer.toString(counter1);
    	numAcc--;
    	counter0++;
    }
    
    for (int i=1;i<array.length;i++){
    	
    	string=array[i];
    	
    	int a=Integer.parseInt(array[i].substring(array[i].length()-1,array[i].length()));
    	
    	int j;
    	for (j=i-1;j>=0;j--){
    		
    		int b=Integer.parseInt(array[j].substring(array[j].length()-1,array[j].length()));
            
    		if(a>b){
    			array[j+1]=array[j];
    		}
    		else break;
    	}
    	array[j+1]=string;
    }
    for (int i=0;i<array.length;i++){
    	System.out.println();
        
    System.out.println(array[i].substring(0,array[i].length()-1));
    
    }
    
	}
	
}